package com.six.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six.core.entity.Area;
import com.six.core.entity.Emp;
import com.six.core.mapper.AreaMapper;
import com.six.core.service.AreaService;
import com.six.core.vo.AreaVo;
import com.six.core.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * @Description:
 * @author:LiWei
 * @date:2023年02月28日9:10
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {
    @Autowired
    private AreaMapper areaMapper;

    @Override
    public Result findAll(Integer currentPage, Integer pageSize, AreaVo areaVo) {
        Page<Area> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        wrapper.like(areaVo.getName()!=null ,"name",areaVo.getName());
        wrapper.like(areaVo.getState()!=null ,"state",areaVo.getState());
        Page<Area> areaPage = areaMapper.selectPage(page,wrapper);
        return new Result(200,"查询成功",areaPage);
    }

    @Override
    public Result addArea(Area area) {
        int insert = areaMapper.insert(area);
        if(insert>0){
            return new Result(200,"添加成功",insert);
        }else {
            return new Result(500,"添加失败",insert);
        }
    }

    @Override
    public Result updateArea(Area area) {
        int i = areaMapper.updateById(area);
        if(i>0){
            return new Result(200,"修改成功",i);
        }else {
            return new Result(500,"修改失败",i);
        }
    }

    @Override
    public Result deleteById(Integer id) {
        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        int i = areaMapper.delete(wrapper);
        if(i>0){
            return new Result(200,"删除成功",i);
        }else {
            return new Result(500, "删除失败", i);
        }
    }

    @Override
    public Result findAllEmp() {
        List emp=areaMapper.findAllEmp();
        return new Result(200,"查询成功!",emp);
    }

    @Override
    public Result exitEmp(Integer id) {
        Area area = areaMapper.selectById(id);
        String empid = area.getEmpid();
        if(empid!=null){
            return new Result(500,"该区已经有管理员了",null);

        }
        return new Result(200,"可以分配",null);
    }

    @Override
    public Result confirmEmp(Integer areaid, Emp emp) {
        Area area=new Area();
        area.setId(areaid);
        area.setEmpid(String.valueOf(emp.getId()));
        areaMapper.updateById(area);
        return new Result(200,"分配管理员成功!",null);
    }

    @Override
    public Result findEmp(Integer id) {
        List<Map> area = areaMapper.findEmp(id);
        return new Result(200,"查询成功",area);
    }

    @Override
    public Result findAllArea() {
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        List<Area> areaList = areaMapper.selectList(queryWrapper);
        return new Result(200,"查询成功",areaList);
    }
}















